<template>
  <div id="app" :class="{ 'mobile': appStore.isMobile }">
    <!-- 全局加载遮罩 -->
    <div v-if="appStore.loading" class="global-loading">
      <div class="loading-spinner">
        <div class="spinner"></div>
        <p>加载中...</p>
      </div>
    </div>

    <!-- 主应用布局 -->
    <div class="app-layout">
      <!-- 头部导航 -->
      <AppHeader />
      
      <!-- 主内容区域 -->
      <main class="main-content">
        <router-view v-slot="{ Component, route }">
          <transition name="page" mode="out-in">
            <component :is="Component" :key="route.path" />
          </transition>
        </router-view>
      </main>
      
      <!-- 底部 -->
      <AppFooter />
    </div>

    <!-- 回到顶部按钮 -->
    <transition name="fade">
      <div 
        v-if="showBackTop" 
        class="back-to-top" 
        @click="scrollToTop"
      >
        <el-icon><ArrowUp /></el-icon>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { ArrowUp } from '@element-plus/icons-vue'
import { useAppStore } from '@/stores'
import AppHeader from '@/components/layout/AppHeader.vue'
import AppFooter from '@/components/layout/AppFooter.vue'

const router = useRouter()
const appStore = useAppStore()
const showBackTop = ref(false)

// 滚动监听
const handleScroll = () => {
  showBackTop.value = window.scrollY > 300
}

// 回到顶部
const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth'
  })
}

// 路由变化监听
router.afterEach((to, from) => {
  // 页面切换时滚动到顶部
  nextTick(() => {
    window.scrollTo(0, 0)
  })
  
  // 更新页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - ${import.meta.env.VITE_APP_TITLE}`
  }
})

onMounted(() => {
  // 初始化应用设置
  appStore.initializeApp()
  
  // 添加滚动监听
  window.addEventListener('scroll', handleScroll)
  
  // 添加窗口大小变化监听
  window.addEventListener('resize', appStore.handleResize)
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
  window.removeEventListener('resize', appStore.handleResize)
})
</script>

<style>
/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background-color: var(--el-bg-color);
  color: var(--el-text-color-primary);
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 应用布局 */
.app-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  flex: 1;
}

.main-content {
  flex: 1;
  padding-top: 80px; /* 为固定头部留出空间 */
}

/* 移动端适配 */
.mobile .main-content {
  padding-top: 60px;
}

/* 全局加载遮罩 */
.global-loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.loading-spinner {
  text-align: center;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #409eff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 页面切换动画 */
.page-enter-active,
.page-leave-active {
  transition: all 0.3s ease;
}

.page-enter-from {
  opacity: 0;
  transform: translateX(20px);
}

.page-leave-to {
  opacity: 0;
  transform: translateX(-20px);
}

/* 回到顶部按钮 */
.back-to-top {
  position: fixed;
  right: 30px;
  bottom: 30px;
  width: 50px;
  height: 50px;
  background: var(--el-color-primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  z-index: 1000;
}

.back-to-top:hover {
  background: var(--el-color-primary-light-3);
  transform: translateY(-2px);
}

.back-to-top .el-icon {
  color: white;
  font-size: 20px;
}

/* 移动端回到顶部按钮 */
@media (max-width: 768px) {
  .back-to-top {
    right: 20px;
    bottom: 20px;
    width: 45px;
    height: 45px;
  }
}

/* 淡入淡出动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式断点 */
@media (max-width: 1200px) {
  .container {
    max-width: 960px;
  }
}

@media (max-width: 992px) {
  .container {
    max-width: 720px;
  }
}

@media (max-width: 768px) {
  .container {
    max-width: 540px;
    padding: 0 15px;
  }
}

@media (max-width: 576px) {
  .container {
    max-width: 100%;
    padding: 0 10px;
  }
}
</style>